<?php
include_once(dirname(__FILE__)."/../../libs/string/libstr.php");
include_once(dirname(__FILE__)."/../../libs/dbclass/dbconn.php");

class dbservice extends dbconn
{
	public $depth=0;

	function getchild($tb,$pid,$select="",$selectcol="ID",$charstr=".",$selwhere=""){
		$this->depth=$this->depth+1;
		if($selwhere!=""){
			if($this->depth==1)
			{
				$selwhere=" ".$selwhere." and  ";
			}else {
				$selwhere="";
			}
		}
		$sqlstr="select * from ".$tb." where ".$selwhere." pid=".$pid."  order by classorder asc, ID asc ";
		$getrs=parent::ds($sqlstr);

		if(count($getrs)>0){
			foreach ($getrs as $rs1)
			{
				if($select==trim($rs1[$selectcol]))
				{
					$selected="selected";
				}
				echo "<option value='".trim($rs1["ID"])."' ".$selected.">".$this->mystring($this->depth,$charstr).trim($rs1["classname"])."</option>";
				$selected="";
				$this->getchild($tb,$rs1["ID"],$select,$selectcol,$charstr,$selwhere);//递归获取子集
			}
		}
		$this->depth=$this->depth-1;
	}

	
	function mystring($str,$str1)
	{
		$tx = "";
		for ($i = 0; $i < $str-1; $i++)
		{

			if ($str > 1)
			{

				$tx .= $str1 . $str1;
			}
		}

		return $tx;
	}

	
	function getclass($tb,$pid,$charstr="..",$file_add="product_class_creat.php",$file_eidt="product_class_edit.php",$file_show="",$selwhere="")
	{
		$this->depth=$this->depth+1;
		if($selwhere!="")
		{
			if($this->depth==1)
			{
				$selwhere=" ".$selwhere." and  ";
			}else
			{
				$selwhere="";
			}
		}
		$sqlstr="select * from ".$tb." where ".$selwhere." pid=".$pid."  order by classorder asc, ID asc ";
		$getrs=parent::ds($sqlstr);
		foreach ($getrs as $rs1)
		{
			echo "<tr bgcolor=\"#ffffff\" onmouseover=\"this.style.backgroundColor='#E7FEFA';\" onmouseout=\"this.style.backgroundColor='';\" >
					<td width=\"49%\" height=\"40\" align=\"left\" style='padding-left:10px;'>".$this->mystring($this->depth,$charstr).$this->classfileshow($rs1["classname"],$file_show)."<input name='getid[]' type='hidden' id='getid[]' value='".$rs1["ID"]."' /></td>
					<td height=\"40\" colspan=\"-4\" align=\"center\"><div align=\"center\">
						<input name=\"classorder".$rs1["ID"]."\" type=\"text\" size=\"5\" value=\"".$rs1["classorder"]."\"></div></td>
					<td height=\"40\" colspan=\"-3\" align=\"center\">
						<bdo><input type=\"button\" name=\"add\" id=\"add\" onclick=\"location.href='".$file_add."?id=".$rs1["ID"]."&order=".$rs1["classorder"]."&classid=".$_GET['classid']."'\" value=\"添加子项\" /></bdo>
					  <bdo><input type=\"button\" name=\"edit\" id=\"edit\" onclick=\"location.href='".$file_eidt."?id=".$rs1["ID"]."&order=".$rs1["classorder"]."&classid=".$_GET['classid']."'\" value=\"修改\" /></bdo>   
					  <bdo><input type=\"button\" name=\"del\" id=\"del\" onclick=\"if(confirm('确定删除本项吗？')==false){return;};location.href='?del=".trim($rs1["rootid"])."&classid=".$_GET['classid']."';\" value=\"删除\" /></bdo>
					</td>
				</tr>";
			
             
			
			$this->getclass($tb,$rs1["ID"],$charstr,$file_add,$file_eidt,$file_show,$selwhere);
		}
		$this->depth=$this->depth-1;
	}

	function getclasspower($tb,$pid,$charstr="..",$file_add="about_class_creat.php",$file_eidt="about_class_edit.php",$file_show="",$selwhere=""){
		$this->depth=$this->depth+1;
		if($selwhere!="")
		{
			if($this->depth==1)
			{
				$selwhere=" ".$selwhere." and  ";
			}else
			{
				$selwhere="";
			}
		}
		$sqlstr="select * from ".$tb." where ".$selwhere." pid=".$pid."  order by classorder asc, ID asc ";
		$getrs=parent::ds($sqlstr);
		foreach ($getrs as $rs1)
		{
			echo "<tr bgcolor=\"#ffffff\" onmouseover=\"this.style.backgroundColor='#E7FEFA';\" onmouseout=\"this.style.backgroundColor='';\"><td style='border-bottom:1px; border-bottom-color:#CCCCCC; border-bottom-style:dashed'>&nbsp;&nbsp;".$this->mystring($this->depth,$charstr).$this->classfileshow($rs1["classname"],$file_show)."<input name='getid[]' type='hidden' id='getid[]' value='".$rs1["ID"]."' /></td><td style='border-bottom:1px; border-bottom-color:#CCCCCC; border-bottom-style:dashed'><div align=\"center\"><input name=\"classorder".$rs1["ID"]."\" type=\"text\" size=\"5\" value=\"".$rs1["classorder"]."\"></div></td><td style='border-bottom:1px; border-bottom-color:#CCCCCC; border-bottom-style:dashed'><div align=\"center\">  <a href=".$file_eidt."?id=".$rs1["ID"]."&order=".$rs1["classorder"]."&classid=".$_GET['classid'].">设置权限</a>   <a href=".$file_eidt."?id=".$rs1["ID"]."&order=".$rs1["classorder"]."&classid=".$_GET['classid'].">后台菜单</a>   <a href=?del=".trim($rs1["rootid"])."&classid=".$_GET['classid']." onclick=\"GP_popupConfirmmsg('您真的要删除吗？');return document.MM_returnValue\">删除本项</a></div></td></tr>";
			$this->getclass($tb,$rs1["ID"],$charstr,$file_add,$file_eidt,$file_show,$selwhere);
		}
		$this->depth=$this->depth-1;
	}

	function classfileshow($str,$url)
	{
		if($url!="")
		{
			return  "<a href=".$url.">".$str."</a>";
		}
		return $str;
	}
	//获得当前路径
	function mappath($tb,$url,$index,$arrstr="",$rew=false,$islink=true,$statid=0,$fuhao=" > ",$color="red",$nokey=true)
	{
		$mc=new libstr();
		if($nokey==true){
		if(trim($mc->Ismp($_GET["keyword"]))!="")
		{
			return "您输入关键字<font color=".$color.">".urldecode($_GET["keyword"])."</font>,查找结果如下：";
		}
		}
		if($url=="")
		{
			return $index;
		}
		$myid=$this->myrootid($tb,$url);
		$myid=substr($myid,2,strlen(trim($myid))-3);
		$tx=split(",",$myid);
		//echo $myid;
		$myjieguo="";
		$num=0;
		$myfuhao=$fuhao;
		for($i=$statid;$i<count($tx);$i++)
		{
			$num=$num+1;
			$sqlstr="select ID,classname,rootid from ".$tb." where ID in('".$tx[$i]."')";
			$rs1=parent::ds($sqlstr);
			if(count($rs1)>0)
			{
				if($i==0&&$index=="")
				{
					$myfuhao="";
				}else
				{
					$myfuhao=$fuhao;
				}

				if(trim($tx[$i])==trim($mc->Ismp($url)))
				{
					$myjieguo.=$myfuhao."<font color=".$color.">".$rs1[0]["classname"]."</font>";
				}else
				{
					if($islink==true)
					{
						$myjieguo.=$myfuhao."".$this->showurl($i,$rs1[0]["classname"],$rs1[0]["ID"],$arrstr,$rew)."";
					}else
					{
						$myjieguo.=$myfuhao."".$rs1[0]["classname"]."";
					}
				}

			}
			else{

			}

		}
		$myjieguo=$index.$myjieguo;
		return  $myjieguo;
	}
	function showurl($i,$str,$id,$arr="",$rew=false)
	{
		global $rewrite,$rewritefile;
		$tx="";
		$mytx="";
		$index="";
		$mytx=split(",",$arr);
		if(count($mytx)<=1)
		{
			$index=$mytx[0];
		}else
		{
			$index=$mytx[$i];
		}

		$tx="<a href='".$index."?cataid=".$id."'>".$str."</a>";
		if($rewrite==true && $rew==true)
		{
			$tx=$index."?cataid=".$id;
			$tx=str_replace(".php?&","_",$tx);
			$tx=str_replace(".php?","_",$tx);
			$tx=str_replace("&","_",$tx);
			$tx=str_replace("=","_",$tx);
			$tx="<a href='".$tx.$rewritefile."'>".$str."</a>";;
		}

		return $tx;
	}
	function myrootid($str,$ur)
	{
		if (isset($ur)&&$ur!=""){

			$sqlstr="select rootid from ".$str." where ID ='".($ur)."'";
			$rs1=parent::dvstr($sqlstr);
			return trim($rs1["rootid"]);
		}
		else
		{
			return "0,";
		}
	}
	

	//获得类名
	function showtitle($str,$tb,$rootid="rootid",$jieguo="")
	{

		$sqlstr="select classname from ".$tb." where ".$rootid."  ='".$str."' ";

		$rs1=parent::ds($sqlstr);
		$rowcount=count($rs1);
		//$jieguo="";
		if($rowcount>0){
			$jieguo= $rs1[0]["classname"];
		}
		return $jieguo;
	}
	function  rootidtocataid($str)
	{
		if(trim($str)=="")
		{
			return "";
		}
		$tx=split(",",$str);
		return $tx[count($tx)-2];
	}
	function getcataid($str)
	{
		if(trim($str)=="")
		{
			return "";
		}
		$tx=split(",",$str);
		$k=count($tx);

		return $tx[$k-2];

	}
	function titlestr($str)
	{
		$mystr=parent::dvstr("select ".$str." from base_content where stat='ok'");
		return $mystr[$str];
	}
	function proindex($col,$tb,$wherestr="isshow>-1",$shownum=5)
	{

		$rs=parent::ds("select ".$col." from  ".$tb." where ".$wherestr." limit ".$shownum);
		return $rs;
	}
	function protuijie($sql,$tb="product_content",$col="*")
	{

		$rs=parent::ds($sql);
		$tx="";
		$myrs= array();
		$i=0;
		foreach ($rs as $r)
		{
			
			$myrs[$i]=parent::dvstr("select ".$col." from ".$tb." where id =".$r["getid"]);
			$myrs[$i]["index"]=$i;
			$i++;
		}
//print_r($myrs);
	return $myrs;
	}
	function xiangguan($inputstr,$outint=10,$tb="product_content",$rootid="",$ziduan="biaoqian,topicname",$tiaojian1="or",$tiaojian2="or",$fuhao=",")
	{
		global $mc;
		$mysqlstr="";
		$mysqlstr=$mc->slsql(trim($inputstr),$ziduan,$tiaojian1,$tiaojian2,$fuhao);
		if($mysqlstr!="")
		{
			if($rootid=="")
			{
				$mysqlstr=$mysqlstr."  and ";
			}else
			{
				$mysqlstr=$mysqlstr."  and rootid like '%".$rootid."%' and ";
			}
		}
		else
		{

			$mysqlstr=" rootid like '%".$rootid."%' and ";
		}

		$newsql="select * from ".$tb." where ".$mysqlstr." id<>".trim($_GET["id"])." and isshow>=9  order by id asc limit ".$outint."";
		//echo $newsql;
		return parent::ds($newsql);

	}
	function  gethistory($id,$num=5,$miao=3600000,$sestr="pro_his1")
	{
		global $mc;
		$mc->sethistory($id,$num,$sestr);
		if($mc->Ismp($_SESSION[$sestr])!="")
		{
			$kvtx="";
			foreach($_SESSION[$sestr] as $cv)
			{
				$kvtx .=",".$cv;
			}
			//echo strlen($kvtx);
			$kvtx=substr($kvtx,1,strlen($kvtx));
			//setcookie("pro_his", $kvtx, time() + (3600*24*30));
			setcookie($sestr, $kvtx, time() + ($miao));
		}
		$rsarr=array();
		if($mc->Ismp($_COOKIE[$sestr])!="")
		{

			$hst=split(",",$_COOKIE[$sestr]);
			for($ik=0;$ik<count($hst);$ik++)
			{
				if($hst[$ik]!="")
				{
					$rsarr[$ik]=parent::dvstr("select topicname,price_m,id,a1,pic from product_content where id=".$hst[$ik]."");
				}}

		}
		//print_r($rsarr);
		return $rsarr;
	}
	
	
	function isroot($str,$tp=0,$limit=0,$item="root"){
       // echo $str."ll";
		//$str=trim(str_replace("0,","",$str));
		$str=substr($str,2,(strlen(trim($str))-1));
		if($str=="")return "";
		$ar=split(",",$str);
		$out="";
		//echo $str."ll";
		for ($i=0;$i<count($ar);$i++){
			if($limit!=0)
			{
				if($i>=$limit)continue;
			}
			if($ar[$i]!="")
			{
				
			
			if($tp==0)
			{
				$out.=",".$item.($i+1);
			}else 
			{
				$out.=",".$ar[$i]."";
			}
			}
			
		}
		$out=substr($out,0,strlen($out));
		return "".$out."";
	}
	
	
}





?>